package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.linlinjava.litemall.db.dao.LitemallCommodityCommentMapper;
import org.linlinjava.litemall.db.domain.LitemallCommodityComment;
import org.linlinjava.litemall.db.domain.LitemallCommodityCommentExample;
import org.linlinjava.litemall.db.util.BigDecimalUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
public class LitemallCommodityCommentService {
    @Resource
    LitemallCommodityCommentMapper dao;

    final BigDecimal MAX_REVIEW = new BigDecimal(5);
    final BigDecimal MIN_REVIEW = new BigDecimal(0);

    public PageInfo<LitemallCommodityComment> selectByCommodity(int page, int pageSize, int commodityId) {
        PageHelper.startPage(page, pageSize);
        return new PageInfo<>(dao.listByCommodity(commodityId));
    }

    public Map<String, BigDecimal> selectInfoByCommodity(int commodityId) {
        return dao.commodityInfo(commodityId);
    }


    /**
     *         "averageValue": 2.5,
     *         "count1": 0,
     *         "count2": 0,
     *         "count0": 1,
     *         "totalNum": 2,
     *         "count3": 1,
     *         "count4": 0,
     *         "persent2": 0,
     *         "persent1": 0,
     *         "persent4": 0,
     *         "persent3": 0.5,
     *         "persent0": 0.5
     * @param list
     * @return
     */
    public Map<String, Object> selectInfoByCommodity(PageInfo<LitemallCommodityComment> list) {
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("list", list.getList());
        long totalNum = list.getTotal();
        result.put("totalNum", totalNum);
        //总和分， 用以计算平均分
        BigDecimal sum = new BigDecimal(0);
        //4分以上评分数量
        int total4 = 0;
        //3分以上评分数量
        int total3 = 0;
        //2分以上评分数量
        int total2 = 0;
        //1分以上评分数量
        int total1 = 0;
        //0分以上评分数量
        int total0 = 0;
        for (LitemallCommodityComment comment : list.getList()) {
            BigDecimal score = comment.getReview();
            sum = sum.add(score);
            if (BigDecimalUtil.biggerThan(score, 4)) {
                total4 ++;
                continue;
            }
            if (BigDecimalUtil.biggerThan(score, 3)) {
                total3 ++;
                continue;
            }
            if (BigDecimalUtil.biggerThan(score, 2)) {
                total2 ++;
                continue;
            }
            if (BigDecimalUtil.biggerThan(score, 1)) {
                total1 ++;
                continue;
            }
            total0 ++;
        }
        //平均分
        BigDecimal averageReview = sum.divide(new BigDecimal(totalNum), 2, BigDecimal.ROUND_HALF_UP);
        //4分以上占比
        double present4 = BigDecimalUtil.div(total4, totalNum, 2);
        //3分以上占比
        double present3 = BigDecimalUtil.div(total3, totalNum, 2);
        //2分以上占比
        double present2 = BigDecimalUtil.div(total2, totalNum, 2);
        //1分以上占比
        double present1 = BigDecimalUtil.div(total1, totalNum, 2);
        //0分以上占比
        double present0 = BigDecimalUtil.div(total0, totalNum, 2);

        //平均分
        result.put("averageReview", averageReview);

        //好评数量
        result.put("total4", total4);
        result.put("good", total4);

        //中评数量
        result.put("total3", total3);
        result.put("total2", total2);
        result.put("mid", total3 + total2);

        //差评数量
        result.put("total1", total1);
        result.put("total0", total0);
        result.put("bad", total1 + total0);


        //好评比例
        result.put("present4", present4 * 100);
        result.put("presentGood", present4 * 100);

        //中评比例
        result.put("present3", present3 * 100);
        result.put("present2", present2 * 100);
        result.put("presentMid", present3 * 100 +  present2 * 100);

        //差评比例
        result.put("present1", present1 * 100);
        result.put("present0", present0 * 100);
        result.put("presentBad", present1 * 100 + present0 * 100);

        return result;
    }

    public void insert(LitemallCommodityComment comment) {
        BigDecimal review = comment.getReview();
        if (review.compareTo(MAX_REVIEW) > 0) {
            review = MAX_REVIEW;
            comment.setReview(review);
        }
        else {
            if (review.compareTo(MIN_REVIEW) < 0) {
                review = MIN_REVIEW;
                comment.setReview(review);
            }
        }
        dao.insertSelective(comment);
    }

    public int update(LitemallCommodityComment litemallCommodityComment) {
        return dao.updateByPrimaryKey(litemallCommodityComment);
    }

    public LitemallCommodityComment selectById(Integer id) {
        return dao.selectByPrimaryKey(id);
    }
    //查::商品ID  .时间短查询
    public List<LitemallCommodityComment>  byCommodityId(Integer commodityId ,LocalDateTime beginTime, LocalDateTime endTime){
        LitemallCommodityCommentExample example = new LitemallCommodityCommentExample();
        LitemallCommodityCommentExample.Criteria criteria = example.createCriteria();
        if(null != commodityId){
             criteria.andCommodityIdEqualTo(commodityId).andIsDeletedEqualTo(0);
        } if(null != beginTime || null != endTime){
            criteria.andCreateTimeBetween(beginTime,endTime).andIsDeletedEqualTo(0);
        }

        List<LitemallCommodityComment> commodityComments = dao.selectByExampleSelective(example);
        return commodityComments;
    }

    public List<LitemallCommodityComment>  byCommodityId(Integer commodityId){
        LitemallCommodityCommentExample example = new LitemallCommodityCommentExample();
        LitemallCommodityCommentExample.Criteria criteria = example.createCriteria();
        if(null != commodityId){
            criteria.andCommodityIdEqualTo(commodityId).andIsDeletedEqualTo(0);
        }
        List<LitemallCommodityComment> commodityComments = dao.selectByExampleSelective(example);
        return commodityComments;
    }
    public List<LitemallCommodityComment>  byCommodityId(){
        LitemallCommodityCommentExample example = new LitemallCommodityCommentExample();
        example.or().andIsDeletedEqualTo(0);
        List<LitemallCommodityComment> commodityComments = dao.selectByExampleSelective(example);
        return commodityComments;
    }


}
